package com.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.ssm.entity.SysUser;
import com.ssm.service.SysUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.Date;
import java.util.List;


/**
 * (SysUser)表控制层
 *
 * @author makejava
 * @since 2021-06-01 10:41:33
 */

@RestController
@RequestMapping("sysUser")
public class SysUserController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public SysUser selectOne(Integer id) {
        return this.sysUserService.queryById(id);
    }

    @RequestMapping("list")
    public PageInfo<SysUser> queryAll(SysUser sysUser,Integer rows,Integer page){
        /*分页组件*/
        PageHelper.startPage(page,rows);
        if (sysUser.getUserName()!=null) {
            sysUser.setUserName("%" + sysUser.getUserName() + "%");
        }
        /*查询所有信息*/
        List<SysUser> list = sysUserService.queryAllByNull(sysUser);

        PageInfo<SysUser> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @RequestMapping("add")
    public String addUser(SysUser sysUser) {
        SysUser sysUser1 = sysUserService.queryByCode(sysUser.getUserCode());
        if (sysUser.getUserCode()==""){
            return "1";
        }
        if (sysUser.getUserName()==""){
            return "2";
        }
        if (sysUser1 !=null ){
            return "3";
        }
        if (sysUser.getUserPwd()==""){
            return "4";
        }

        sysUser.setCreateTime(new Date());
        sysUser.setUpdateTime(new Date());
        sysUser.setSalt(0);
        sysUser.setLocked(1);
        sysUserService.insert(sysUser);
        return "success";

    }
    @RequestMapping("update")
    public String updateUser(SysUser sysUser) {
        System.out.println(sysUser);
        if (sysUser.getUserCode()==""){
            return "1";
        }
        if (sysUser.getUserName()==""){
            return "2";
        }
        else {
            sysUser.setUpdateTime(new Date());
            sysUserService.update(sysUser);
            return "success";
        }
    }
    @RequestMapping("query/{id}")
    public SysUser queryUserById(@PathVariable  Integer id){
        SysUser sysUser=sysUserService.queryById(id);
        return sysUser;
    }

    @RequestMapping("del/{id}")
    public  String delUser(@PathVariable  Integer id){

        sysUserService.deleteById(id);
        return  "success";
    }



    @RequestMapping("dels/{ids}")
    public  String delUsers(@PathVariable  Integer[] ids){
        sysUserService.deleteByIds(ids);
        return  "success";
    }

    @RequestMapping("queryAll")
    public List<SysUser> queryAll(SysUser sysUser){
        return sysUserService.queryAll(sysUser);
    }

}
